﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using Telerik.Web.UI;
using System.Collections.Generic;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTCategoryNewsCopy_Move : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String Loai = "";
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private DataTable dtProductCategorys = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dtProductCategorys = DanhMucTinTucProvider.GetDanhMucTinTuc().Tables[0]; ;

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    Loai = Request.QueryString["Type"].ToString();
                    Session["Type"] = Loai;

                }
                catch (Exception ex)
                {
                }
                if (Loai == "Move")
                {
                    if (IsVN == true)
                        lbMove_Copy.Text = HangSo.Move_VN;
                    else
                        lbMove_Copy.Text = HangSo.Move_Other;
                }
                if (Loai == "Copy")
                {
                    if (IsVN == true)
                        lbMove_Copy.Text = HangSo.Copy_VN;
                    else
                        lbMove_Copy.Text = HangSo.Copy_Other;
                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                CreateDDLCategory(0, 0);
                if (IsVN == true)
                    this.ddlDanhmucTinTuc.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                else
                    this.ddlDanhmucTinTuc.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));               
            }
        }

        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;

            for (Int32 i = 0; i < amountOfCategorySub; ++i)
            {
                Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucTinTucID"].ToString());
                ListItem li;
                if (IsVN == true)
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucVN"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                else
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucOther"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                this.ddlDanhmucTinTuc.Items.Add(li);
                CreateDDLCategory(subCategoryID, level + 1);
            }
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbSave.Text = rm.GetString("lbSave", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbCategory.Text = rm.GetString("lbCategory", ci);
            lbChon.Text = rm.GetString("lbChon", ci);            
        }

        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            // lấy danh mục sản phẩm theo Danh mục ID ở dropdowlist
            DanhMucTinTuc theDanhMucCha = DanhMucTinTucProvider.GetDanhMucTinTucByDanhMucID(Int32.Parse(ddlDanhmucTinTuc.SelectedValue));

            // nếu là copy
            if (Session["Type"].ToString() == "Copy")
            {
                // lấy giá trị trên sesion  gán vào mảng
                ArrayList arr = (ArrayList)Session["ArrayListID"];
                for (int i = 0; i < arr.Count; i++)
                {
                    // lấy đối tượng danh mục sản phẩm theo danhmucID trong array 
                    DanhMucTinTuc theDanhMucTinTuc = DanhMucTinTucProvider.GetDanhMucTinTucByDanhMucID(Int32.Parse(arr[i].ToString()));

                    //khoi tao đối tượng danh mục sản phẩm
                    DanhMucTinTuc theDanhMuc = new DanhMucTinTuc();
                    theDanhMuc.DanhMucTinTucID = -1;
                    theDanhMuc.Icon = theDanhMucTinTuc.Icon;
                    theDanhMuc.LuotTruyCap = theDanhMucTinTuc.LuotTruyCap;
                    theDanhMuc.ChuyenMucChaID = theDanhMucCha.DanhMucTinTucID;                    

                    theDanhMuc.SapXep = theDanhMucTinTuc.SapXep;
                    theDanhMuc.TenDanhMucTinTucOther = theDanhMucTinTuc.TenDanhMucTinTucOther;
                    theDanhMuc.TenDanhMucTinTucVN = theDanhMucTinTuc.TenDanhMucTinTucVN;
                    Int32 DanhMucOut = DanhMucTinTucProvider.IUDanhMucTinTuc(theDanhMuc, true);// insert vào database

                    //Nếu danh mục được chọn có danh mục con thì copy cả danh mục con vào danh mục mới
                    DanhMucCon(DanhMucOut, theDanhMucTinTuc.DanhMucTinTucID);

                }

            }
            try
            {
                if (Session["Type"].ToString() == "Move")
                {
                    ArrayList arr = (ArrayList)Session["ArrayListID"];
                    for (int i = 0; i < arr.Count; i++)
                    {
                        DanhMucTinTuc theDanhMucTinTuc = DanhMucTinTucProvider.GetDanhMucTinTucByDanhMucID(Int32.Parse(arr[i].ToString()));

                        DanhMucTinTuc theDanhMuc = new DanhMucTinTuc();
                        theDanhMuc.DanhMucTinTucID = theDanhMucTinTuc.DanhMucTinTucID;
                        theDanhMuc.Icon = theDanhMucTinTuc.Icon;
                        theDanhMuc.LuotTruyCap = theDanhMucTinTuc.LuotTruyCap;
                        theDanhMuc.ChuyenMucChaID = theDanhMucCha.DanhMucTinTucID;

                        theDanhMuc.SapXep = theDanhMucTinTuc.SapXep;
                        theDanhMuc.TenDanhMucTinTucOther = theDanhMucTinTuc.TenDanhMucTinTucOther;
                        theDanhMuc.TenDanhMucTinTucVN = theDanhMucTinTuc.TenDanhMucTinTucVN;
                        Int32 DanhMucOut = DanhMucTinTucProvider.IUDanhMucTinTuc(theDanhMuc, false);// insert vào database

                        //Nếu danh mục được chọn có danh mục con thì copy cả danh mục con vào danh mục mới
                        DanhMucCon(DanhMucOut, theDanhMucTinTuc.DanhMucTinTucID);
                    }
                }               
            }
            catch
            {
                if (IsVN == true)
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Move_ErorrVN);
                else
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Move_ErorrOther);
                return;
            }
            Response.Redirect("EHTCategoryNews.aspx");
        }


        public void DanhMucCon(Int32 DanhMucOut, Int32 DanhMucID)
        {
            DataSet ds = DanhMucTinTucProvider.GetDanhMucTinTucsByParentID(DanhMucID); // lấy tất cả danh mục con theo ID danh mục cha 

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DanhMucTinTuc theDanhMucCon = new DanhMucTinTuc();
                    theDanhMucCon.ChuyenMucChaID = DanhMucOut;
                    theDanhMucCon.Icon = ds.Tables[0].Rows[i]["Icon"].ToString();
                    theDanhMucCon.LuotTruyCap = Int32.Parse(ds.Tables[0].Rows[i]["LuotTruyCap"].ToString());
                    theDanhMucCon.SapXep = Int32.Parse(ds.Tables[0].Rows[i]["SapXep"].ToString());
                    theDanhMucCon.TenDanhMucTinTucOther = ds.Tables[0].Rows[i]["TenDanhMucTinTucOther"].ToString();
                    theDanhMucCon.TenDanhMucTinTucVN = ds.Tables[0].Rows[i]["TenDanhMucTinTucVN"].ToString();                  
                    if (Session["Type"].ToString() == "Copy")
                    {
                        theDanhMucCon.DanhMucTinTucID = -1;
                        DanhMucTinTucProvider.IUDanhMucTinTuc(theDanhMucCon, true);
                    }
                    if (Session["Type"].ToString() == "Move")
                    {
                        theDanhMucCon.DanhMucTinTucID = Int32.Parse(ds.Tables[0].Rows[i]["DanhMucTinTucID"].ToString());
                        DanhMucTinTucProvider.IUDanhMucTinTuc(theDanhMucCon, false);
                    }
                }
            }
        }
    }
}